package com.entities;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Twitch {

	public static final String fetchUrl = "https://api.twitch.tv/kraken/streams?game=Dota%202";
	public static List<Twitch> listTwich;

	private long id;
	private int viewers;
	private String preview;
	private String status;
	private String display_name;
	private String url;
	
	public long getId() {
		return id;
	}


	public int getViewers() {
		return viewers;
	}


	public String getPreview() {
		return preview;
	}


	public String getStatus() {
		return status;
	}


	public String getDisplay_name() {
		return display_name;
	}


	public String getUrl() {
		return url;
	}


	public static List<Twitch> getList() {
		return listTwich;
	}
	
	
	
	public Twitch(long id, int viewers, String preview, String status,
			String display_name, String url) {
		super();
		this.id = id;
		this.viewers = viewers;
		this.preview = preview;
		this.status = status;
		this.display_name = display_name;
		this.url = url;
	}
	
	public static List<Twitch> getLiveStreams(JSONObject json) {
		
		if(listTwich==null){
			listTwich = new ArrayList<Twitch>();
	
		try { 
			
			JSONArray ja = json.getJSONArray("streams");
			for(int cont = 0; cont<ja.length(); cont++){
			JSONObject jTw = ja.getJSONObject(cont);
			long id = Long.valueOf(jTw.getString("_id"));
			int viewers = jTw.getInt("viewers");
			String preview = jTw.getJSONObject("preview").getString("medium");
			JSONObject channel = jTw.getJSONObject("channel");
			String status = channel.getString("status");
			String display_name = channel.getString("display_name");
			String url = channel.getString("url");
			
			listTwich.add(new Twitch(id, viewers, preview, status, display_name, url));
					
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		return listTwich;
		
	}



}
